#pragma once
#include "ieventagent.h"
#include <vector>
#include <algorithm>

template<typename Targ>
class EventAgentSupport
{
	std::vector<IEventAgent<Targ>* > eventAgents;
public:

	EventAgentSupport(void)
	{
	}

	virtual ~EventAgentSupport(void)
	{

		for( std::vector<IEventAgent<Targ>* >::const_iterator it = eventAgents.begin(); it != eventAgents.end(); it++)
		{
			(*it)->clearSource();
		}
		eventAgents.clear();
	}

	void registerEvent(IEventAgent<Targ>* eventAgent)
	{
		if(std::find(eventAgents.begin(), eventAgents.end(), eventAgent) == eventAgents.end())
		{
			eventAgents.push_back(eventAgent);
		}

	}

	void unregisterEvent(IEventAgent<Targ>* eventAgent)
	{

		std::vector<IEventAgent<Targ>* >::const_iterator it = std::find(eventAgents.begin(), eventAgents.end(), eventAgent);
		if(it != eventAgents.end())
		{
			eventAgents.erase(it);
		}
	}
	void fireEvent(Targ*  args)
	{

		for(std::vector<IEventAgent<Targ>* >::const_iterator it = eventAgents.begin();
			it!= eventAgents.end(); it++)
		{
			(*it)->fireEvent(args);
		}

	}
};
